<html>
<head>
<title>firstworks   Programming with Rudiments using the standard I/O classes</title>
<link href="../css/styles.css" rel="stylesheet">
</head>
<body>

<span class="heading">Using the standard I/O classes</span><br><br>

<p>The filedescriptor class provides methods for generic input and output to file descriptors.  Rudiments pre-defines three instances: stdinput, stdoutput and stderror, which provide access to standard input, standard output and standard error.</p>

<p>As these classes are ultimately instances of the filedescriptor class, many I/O methods are available, but the most commonly used are read(), write() and printf().  The printf() method features all of the formatting capabilities of the standard C printf() function.</p>

<blockquote class="code">

<script type='text/javascript'>
<!--

-->
</script>
<font face="monospace">
<font color="#800080">#include&nbsp;</font><font color="#ff00ff">&lt;rudiments/stdio.h&gt;</font><br>
<br>
<font color="#2e8b57"><b>int</b></font>&nbsp;main(<font color="#2e8b57"><b>int</b></font>&nbsp;argc,&nbsp;<font color="#2e8b57"><b>const</b></font>&nbsp;<font color="#2e8b57"><b>char</b></font>&nbsp;**argv) {<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">// standard output...</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stdoutput.write(<font color="#ff00ff">&quot;This line is written to standard output.</font><font color="#6a5acd">\n</font><font color="#ff00ff">&quot;</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stdoutput.write(<font color="#ff00ff">&quot;Fully functional printf is also available: &quot;</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stdoutput.printf(<font color="#ff00ff">&quot;</font><font color="#6a5acd">%s</font><font color="#ff00ff">,&nbsp;</font><font color="#6a5acd">%c</font><font color="#ff00ff">,&nbsp;</font><font color="#6a5acd">%05d</font><font color="#6a5acd">\n</font><font color="#ff00ff">&quot;</font>,&nbsp;<font color="#ff00ff">&quot;hello&quot;</font>,<font color="#ff00ff">'c'</font>,<font color="#ff00ff">25</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stdoutput.write(<font color="#6a5acd">'\n'</font>);<br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">// standard error...</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stderror.write(<font color="#ff00ff">&quot;This line is written to standard error.</font><font color="#6a5acd">\n</font><font color="#ff00ff">&quot;</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stderror.write(<font color="#ff00ff">&quot;Fully functional printf is also available: &quot;</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stderror.printf(<font color="#ff00ff">&quot;</font><font color="#6a5acd">%s</font><font color="#ff00ff">,&nbsp;</font><font color="#6a5acd">%c</font><font color="#ff00ff">,&nbsp;</font><font color="#6a5acd">%05d</font><font color="#6a5acd">\n</font><font color="#ff00ff">&quot;</font>,&nbsp;<font color="#ff00ff">&quot;hello&quot;</font>,<font color="#ff00ff">'c'</font>,<font color="#ff00ff">25</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stderror.write(<font color="#6a5acd">'\n'</font>);<br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">// standard input...</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stdoutput.write(<font color="#ff00ff">&quot;Type something and hit return...</font><font color="#6a5acd">\n</font><font color="#ff00ff">&quot;</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>char</b></font>&nbsp;&nbsp;&nbsp;&nbsp;input[<font color="#ff00ff">1024</font>];<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>ssize_t</b></font>&nbsp;count=stdinput.read(input,<font color="#a52a2a"><b>sizeof</b></font>(input));<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input[count]=<font color="#6a5acd">'\0'</font>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stdoutput.printf(<font color="#ff00ff">&quot;You typed:&nbsp;</font><font color="#6a5acd">%s</font><font color="#6a5acd">\n</font><font color="#ff00ff">&quot;</font>,input);<br>
}<br>
</font>
<!-- vim: set foldmethod=manual : -->
</blockquote>

</body>
</html>
